﻿// -----------------------------------------------------------------------
// <copyright file="GeometricMeanFilter.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace IP_Package.Operations.Restoration.RemoveNoise
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using IP_Package.Operations.Neighbor_operations;
    using IPPackage.ImageOperation;

    /// <summary>
    /// TODO: apply geometric mean filter to remove noise.
    /// </summary>
    public class GeometricMeanFilter : Operation
    {
        /// <summary>
        /// generate the Smothings mean mask based on the mask size.
        /// </summary>
        /// <param name="maskSize">Size of the mask.</param>
        /// <returns>the mask</returns>
        public Mask MeanFilter(int maskSize)
        {
            try
            {
                Mask m;
                m.MaskSize = maskSize;
                m.MaskValues = new double[maskSize, maskSize];
                double value = 1.0 / (maskSize * maskSize);
                for (int i = 0; i < maskSize; i++)
                {
                    for (int j = 0; j < maskSize; j++)
                    {
                        m.MaskValues[i, j] = value;
                    }
                }

                return m;
            }
            catch (Exception ex)
            {
                IPPackage.Logger.LogException(ex);
                throw ex;
            }
        }

        /// <summary>
        /// Applies the GM filter.
        /// </summary>
        /// <param name="maskSize">Size of the mask.</param>
        /// <param name="orignalImg">The orignal img.</param>
        /// <returns>the result of apply geometric mean</returns>
        public Bitmap ApplyGMFilter(double maskSize, Color[,] orignalImg)
        {
            LinearConvolution convObj = new LinearConvolution();
            convObj.BufferOfImage = orignalImg;
            this.BufferOfImage = orignalImg;
            Color[,] buffer = convObj.AddBorder(maskSize);
            ////Mask meanMask = this.MeanFilter((int)maskSize);
            Bitmap newImage = convObj.LinearConvolutionGeometricFn(buffer, maskSize);
            this.NewBufferOfImage = convObj.NewBufferOfImage;
            return newImage;
        }
    }
}
